package cn.ctvonline.android.kaopu.widget.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;

/**
 * Created by dujian on 15/12/15.
 */
public class WaterTextView extends TextView {
    static final String TAG = "WaterTextView";
    static final int WATER_STYLE_CENTER = 0;
    static final int WATER_STYLE_POINTER = 1;
    private int waterStyle;
    private int waterRadiusLimit;
    private int widthSize, heightSize;
    private Context context;
    private int waterColor;
    private int centerX, centerY;
    private boolean isPressed = false;
    private int revealDrawGaps = 10;
    private int radius;
    boolean shouldDraw = false;
    private boolean trackEventBeforeWater = true;

    public WaterTextView(Context context) {
        this(context, null);
    }

    public WaterTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaterTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {
        waterColor = Color.LTGRAY;
        waterStyle = WATER_STYLE_POINTER;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            widthSize = measuredWidth;
        } else {
            if (widthMode == MeasureSpec.AT_MOST) {
                widthSize = Math.min(measuredWidth, 0);
            }
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            heightSize = measuredHeight;
        } else {
            if (heightMode == MeasureSpec.AT_MOST) {
                heightSize = Math.min(measuredHeight, 0);
            }
        }
        setMeasuredDimension(widthSize, heightSize);
    }

    private MotionEvent lastEvent;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event == null)
            return true;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                isPressed = true;
                radius = 0;
                Log.i(TAG, "");
                shouldDraw = true;
                waterRadiusLimit = calcuteRadiusLimit(event, widthSize, heightSize);
                Log.i(TAG, "centerX = " + centerX + ",centerY=" + centerY + ", radiusLimit=" + waterRadiusLimit);
            }
            break;
            case MotionEvent.ACTION_UP: {
                postInvalidate();
                isPressed = false;
                if (trackEventBeforeWater && shouldDraw) {
                    lastEvent = event;
                    return true;
                }
            }
            break;
            case MotionEvent.ACTION_CANCEL: {
                isPressed = true;
            }
            break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int revealGap = waterRadiusLimit / (revealDrawGaps);
        if (isPressed) {
            revealGap = waterRadiusLimit / (revealDrawGaps * 5);
        }
        if (shouldDraw) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(waterColor);
            paint.setStyle(Paint.Style.FILL);
            canvas.save();
            canvas.drawCircle(centerX, centerY, radius, paint);
            canvas.restore();
            radius += revealGap;
            if (radius >= waterRadiusLimit) {
                shouldDraw = false;
                postInvalidate();
                postDelayTouchEvent(lastEvent, 50);
            } else {
                postInvalidateDelayed(50);
            }
        }
        super.onDraw(canvas);
    }

    public void postDelayTouchEvent(final MotionEvent event, int millSeconds) {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                dispatchTouchEvent(event);
            }
        }, millSeconds);
    }

    private int calcuteRadiusLimit(MotionEvent event, int width, int height) {
        int result = 0;
        if (waterStyle == WATER_STYLE_CENTER) {
            centerX = width / 2;
            centerY = height / 2;
            result = Math.max(centerX, centerY);
        } else {
            if (waterStyle == WATER_STYLE_POINTER) {
                centerX = (int) event.getX();
                centerY = (int) event.getY();
                if (centerX < width / 2 && centerY < height / 2) {
                    result = (int) Math.sqrt(Math.pow(height - centerY, 2) + Math.pow(width - centerX, 2));
                } else if (centerX < width / 2 && centerY > height / 2) {
                    result = (int) Math.sqrt(Math.pow(centerY, 2) + Math.pow(width - centerX, 2));
                } else if (centerX > width / 2 && centerY > height / 2) {
                    result = (int) Math.sqrt(Math.pow(centerX, 2) + Math.pow(centerY, 2));
                } else {
                    result = (int) Math.sqrt(Math.pow(centerX, 2) + Math.pow(height - centerY, 2));
                }
            }
        }
        return result;
    }
}
